﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HandBook.Data;
using Telerik.Web.UI;
using System.Text;

public partial class Admin_RestManagement : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["holdstate"] != null)
        {
            uxRestList.DataSource = ViewState["holdstate"];
        }
        RadComboBox1.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + btnSearch.UniqueID + "','')");
    }

    protected void uxRestList_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        var restList = from rest in HandBookData.EatDrinkDB.Restaurants
                       select rest;
        uxRestList.DataSource = restList;
    }

    protected void RadComboBox1_DataBound(object sender, EventArgs e)
    {
        RadComboBox1.Text = "Nhập vào từ khóa cần tìm";
    }

    protected void uxRestList_ItemDataBound(object sender, GridItemEventArgs e)
    {
        
    }

    protected void uxRestList_UpdateCommand(object source, GridCommandEventArgs e)
    {
    }

    protected void uxRestList_InsertCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Restaurant rest = new Restaurant();
        rest.RestName = (e.Item.FindControl("txtRestName") as TextBox).Text.Trim();
        rest.RestCat = (e.Item.FindControl("txtRestCat") as TextBox).Text.Trim();
        rest.Address = (e.Item.FindControl("txtAddress") as TextBox).Text.Trim();
        rest.AddressDescription = (e.Item.FindControl("txtAddressDescription") as TextBox).Text.Trim();
        rest.Slogan = (e.Item.FindControl("txtSlogan") as TextBox).Text.Trim();
        rest.ShortDescription = (e.Item.FindControl("txtShortDescription") as TextBox).Text.Trim();
        rest.LongDescription = (e.Item.FindControl("RadEditor1") as RadEditor).Content.Trim();
        rest.LocationID = Convert.ToInt32((e.Item.FindControl("ddlLocation") as DropDownList).SelectedValue);
        rest.PhoneNumber = (e.Item.FindControl("txtPhoneNumber") as TextBox).Text.Trim();
        rest.Email = (e.Item.FindControl("txtEmail") as TextBox).Text.Trim();
        rest.Fax = (e.Item.FindControl("txtFax") as TextBox).Text.Trim();
        rest.Website = (e.Item.FindControl("txtWebsite") as TextBox).Text.Trim();
        rest.SeatCount = (e.Item.FindControl("txtSeatCount") as TextBox).Text.Trim();
        rest.IsOrder = (e.Item.FindControl("ddlIsOrder") as DropDownList).SelectedValue.ToString();
        rest.OffDate = (e.Item.FindControl("txtOffDate") as TextBox).Text.Trim();
        rest.ServedTime = (e.Item.FindControl("txtServedTime") as TextBox).Text.Trim();
        rest.PaymentType = (e.Item.FindControl("txtPaymentType") as TextBox).Text.Trim();
        rest.Language = (e.Item.FindControl("txtLanguage") as TextBox).Text.Trim();
        rest.AveragePrice = (e.Item.FindControl("txtAveragePrice") as TextBox).Text.Trim();
        rest.SubServices = (e.Item.FindControl("txtSubServices") as TextBox).Text.Trim();
        rest.Menu = (e.Item.FindControl("RadEditor2") as RadEditor).Content.Trim();
        rest.Note = (e.Item.FindControl("txtNote") as TextBox).Text.Trim();
        rest.Search = (e.Item.FindControl("txtSearch") as TextBox).Text.Trim();
        rest.ImageFolderName = (e.Item.FindControl("txtImageFolderName") as TextBox).Text.Trim();
        rest.Flash = (e.Item.FindControl("txtFlash") as TextBox).Text.Trim();

        string orderContent = (e.Item.FindControl("txtOrder") as TextBox).Text.Trim();
        if (!orderContent.Equals(""))
        {
            rest.Order = Convert.ToInt32(orderContent);
        }
        else
        {
            rest.Order = null;
        }

        string userIDContent = (e.Item.FindControl("txtUserID") as TextBox).Text.Trim();
        if (!userIDContent.Equals(""))
        {
            rest.UserID = Convert.ToInt32(userIDContent);
        }
        else
        {
            rest.UserID = null;
        }

        rest.IsActivated = Convert.ToBoolean((e.Item.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());
        rest.Url = (e.Item.FindControl("txtUrl") as TextBox).Text.Trim();

        HandBookData.EatDrinkDB.Restaurants.InsertOnSubmit(rest);
        HandBookData.EatDrinkDB.SubmitChanges();
    }

    protected void uxRestList_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int restID = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RestID"]);
        var rest = HandBookData.EatDrinkDB.Restaurants.Single(r => r.RestID == restID);
        //HandBookData.EatDrinkDB.Restaurants.DeleteOnSubmit(rest);
        rest.IsActivated = false;
        HandBookData.EatDrinkDB.SubmitChanges();

        if (ViewState["holdstate"] != null)
        {
            DataTable table = (DataTable)ViewState["holdstate"];
            foreach (DataRow row in table.Rows)
            {
                if (Convert.ToInt32(row["RestID"].ToString()) == restID)
                {
                    row["IsActivated"] = false;
                }
            }
            ViewState["holdstate"] = table;
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        EatDrinkDBDataContext context = new EatDrinkDBDataContext();
        
        string content = RadComboBox1.Text.Trim();
        string specialChar = "-_+*:,.@{}[]()~$#%^&=|;?/<>!";
        int len = specialChar.Length;
        specialChar = specialChar.Trim();
        
        //lọc bỏ các ký tự đặc biệt
        for (int i = 0; i < len; i++)
        {
            if (content.Contains(specialChar[i]))
            {
                content = content.Replace(specialChar[i].ToString(), "");
            }
        }
        
        //tách chuỗi tìm kiếm
        string[] keywords = content.Split(' ');
        string where = string.Empty;
        foreach (string item in keywords)
        {
            if (where.Equals(string.Empty))
            {
                where = "RestName LIKE '%" + item + "%' OR Search LIKE '%" + item + "%'";
            }
            else
            {
                where += " OR RestName LIKE '%" + item + "%' OR Search LIKE '%" + item + "%'";
            }
        }
        string sql = "SELECT * FROM Restaurant WHERE " + where;

        var restList = context.ExecuteQuery<Restaurant>(sql);

        DataTable table = new DataTable();
        table.Columns.Add("RestID");
        table.Columns.Add("RestName");
        table.Columns.Add("RestCat");
        table.Columns.Add("Address");
        table.Columns.Add("ServedTime");
        table.Columns.Add("IsActivated");

        foreach (var item in restList)
        {
            DataRow row = table.NewRow();
            row["RestID"] = item.RestID;
            row["RestName"] = item.RestName;
            row["RestCat"] = item.RestCat;
            row["Address"] = item.Address;
            row["ServedTime"] = item.ServedTime;
            row["IsActivated"] = item.IsActivated;
            table.Rows.Add(row);
        }

        ViewState["holdstate"] = table;

        uxRestList.DataSource = table;
        uxRestList.Rebind();
    }
   
    protected void uxRestList_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "ViewAll")
        {
            var restList = from r in HandBookData.EatDrinkDB.Restaurants
                           select r;

            DataTable table = new DataTable();
            table.Columns.Add("RestID");
            table.Columns.Add("RestName");
            table.Columns.Add("RestCat");
            table.Columns.Add("Address");
            table.Columns.Add("ServedTime");
            table.Columns.Add("IsActivated");

            foreach (var item in restList)
            {
                DataRow row = table.NewRow();
                row["RestID"] = item.RestID;
                row["RestName"] = item.RestName;
                row["RestCat"] = item.RestCat;
                row["Address"] = item.Address;
                row["ServedTime"] = item.ServedTime;
                row["IsActivated"] = item.IsActivated;
                table.Rows.Add(row);
            }

            ViewState["holdstate"] = table;

            uxRestList.DataSource = table;
            uxRestList.Rebind();
        }
    }
}
